package com.leapp.sk.viewdelegate.adapter

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import cc.vv.lkbasecomponent.util.LKStringUtil
import cc.vv.lkimcomponent.lkim.bean.LKIMConversation
import cc.vv.lkimcomponent.lkim.bean.body.LKIMTxtBody
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMChatType
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.baselibrary.db.dao.AccountDao
import com.example.baselibrary.db.dao.GroupDao
import com.example.mvp.delegate.BaseAdapterDelegate
import com.example.mvp.view.GlideCircleTransform
import com.example.mvp.view.LKCircleImageView
import com.example.util.LKTimeUtil
import com.leapp.sk.R
import com.example.im.server.im.IMChatService
import com.example.util.IntegerUtil
import com.example.util.finallist.IMExtKey
import com.example.util.finallist.SkKey
import com.example.util.finallist.SkKey.Companion.SK_SYSTEM_MSG

/**
 * 消息列表适配器
 * @author  chaohaipeng  on 2018/7/24.
 */
class MessageAdalterDelegate : BaseAdapterDelegate() {

    /**
     * 头像加载操作
     */
    var mHeadRequestOptions: RequestOptions = RequestOptions()

    override fun getOptionsMenuId(): Int {
        return 0
    }

    override fun initWidget() {
    }

    override fun onDestroy() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.adapter_imfragment
    }

    /**
     * 设置数据
     */
    fun setData(context: Context, mLKIMConversation: LKIMConversation) {
        val lastLKIMMessage = mLKIMConversation.lastLKIMMessage
        var nick = lastLKIMMessage.getAttribute(IMExtKey.EXTKEY_NICK, "")
        val avatar = lastLKIMMessage.getAttribute(IMExtKey.EXTKEY_AVATAR, "")
        val messageType = lastLKIMMessage.getAttribute(IMExtKey.EXTKEY_MESSAGETYPE,"")

        val ima_aif_avatar = getView<LKCircleImageView>(R.id.ima_aif_avatar)
        val tv_aif_nick = getView<TextView>(R.id.tv_aif_nick)
        val tv_aif_time = getView<TextView>(R.id.tv_aif_time)
        val tv_aif_value = getView<TextView>(R.id.tv_aif_value)
        val v_mf_stick = getView<View>(R.id.v_mf_stick)
        if (LKIMChatType.SingleChat == lastLKIMMessage.lkimChatType) {
            val conversationId = mLKIMConversation.getConversationId()
            val accountTable = AccountDao.getInstance().queryById(conversationId)
            val nick = if (null == accountTable) LKStringUtil.getString(R.string.app_name) else accountTable.nick
            val avatar = if (null == accountTable) "" else accountTable.avatar
            tv_aif_nick.text = nick
            //系统通知
            if (TextUtils.equals(SK_SYSTEM_MSG,conversationId)){
                val notifyType = lastLKIMMessage.getAttribute(IMExtKey.EXTKEY_NOTIY_TYPE,"")
                val integer = IntegerUtil.getInstance().isInteger(notifyType)
                if (integer){
                    val toInt = notifyType.toInt()
                    setImage(context,ima_aif_avatar,tv_aif_nick, toInt)
                }

            }else{
                setHead(context, avatar, ima_aif_avatar)
            }
        } else {
            val groupTable = GroupDao.getInstance().queryById(mLKIMConversation.conversationId)
            val groupNick = if (null == groupTable) LKStringUtil.getString(R.string.app_name) else groupTable.groupNick
            val groupAvatar = if (null == groupTable) "" else groupTable.groupAvatar
            tv_aif_nick.text = groupNick
            setHead(context, groupAvatar, ima_aif_avatar)
        }
        msgNum(context, mLKIMConversation)
        val formatTime = LKTimeUtil.getInstance().formatTimeA(mLKIMConversation.time)
        tv_aif_time.text = formatTime
        val showText = IMChatService.getInstance().initShowText(mLKIMConversation)
        tv_aif_value.text = showText
        val isStick = mLKIMConversation.getAttribute(IMExtKey.EXTKEY_TOP, SkKey.BTKEY_String_0)
      if (TextUtils.equals(SkKey.BTKEY_String_1, isStick)){
          v_mf_stick.visibility=View.VISIBLE
      }else{
          v_mf_stick.visibility=View.GONE
      }
    }


    /**
     * 设置系统通知左边图片
     */
    private fun setImage(context: Context,image: ImageView,textType:TextView, type: Int) {
        when(type){
            SkKey.BTKEY_INT_1->{
                image.setImageResource(R.mipmap.icon_notify_class_notify)
                textType.text=context.resources.getString(R.string.string_notify_class)
            }
            SkKey.BTKEY_INT_2->{
                image.setImageResource(R.mipmap.icon_task_notify)
                textType.text=context.resources.getString(R.string.string_notify_task)
            }
            SkKey.BTKEY_INT_6->{
                image.setImageResource(R.mipmap.icon_leave_notify)
                textType.text=context.resources.getString(R.string.string_notify_leave)
            }
            SkKey.BTKEY_INT_3->{
                image.setImageResource(R.mipmap.icon_shenhe_notify)
                textType.text=context.resources.getString(R.string.string_notify_exmine_notify)
            }
            SkKey.BTKEY_INT_5->{
                image.setImageResource(R.mipmap.icon_system_notify)
                textType.text=context.resources.getString(R.string.string_notify_system)
            }
        }

    }
    /**
     * 设置消息数量
     */
    private fun msgNum(context: Context, mLKIMConversation: LKIMConversation) {
        val tv_aif_unread = getView<TextView>(R.id.tv_aif_unread)
        val iv_aif_dnd = getView<ImageView>(R.id.iv_aif_dnd)
        // 免打扰
        val isDnd = mLKIMConversation.getAttribute(IMExtKey.EXTKEY_DND, SkKey.BTKEY_String_0)
         if (TextUtils.equals(SkKey.BTKEY_String_1, isDnd)){
             iv_aif_dnd.visibility=View.VISIBLE
             tv_aif_unread.visibility=View.GONE
         }else{
             iv_aif_dnd.visibility=View.GONE
             if (0 != mLKIMConversation.unRreadMsgCount) {
                 if (mLKIMConversation.unRreadMsgCount > 99) {
                     tv_aif_unread.text = "99+"

                 } else {
                     tv_aif_unread.text = mLKIMConversation.unRreadMsgCount.toString()
                 }
                 tv_aif_unread.visibility = View.VISIBLE
             } else {
                 tv_aif_unread.visibility = View.GONE
             }

         }

    }


    /**
     * 设置头像
     */
    private fun setHead(context: Context, headUrl: String?, headImage: LKCircleImageView) {
//
//        val tag = headImage.getTag(R.id.tag_head)
//        if ( tag==null||!tag?.equals(headUrl)) {
            val mRequestManager = Glide.with(context)
            mHeadRequestOptions?.diskCacheStrategy(DiskCacheStrategy.NONE)
            mHeadRequestOptions?.skipMemoryCache(true)
            mHeadRequestOptions?.error(R.mipmap.icon_defout_head)
            mHeadRequestOptions?.transforms(GlideCircleTransform(context))
            mRequestManager?.load(headUrl)
                    ?.thumbnail(0.2f)
                    ?.apply(mHeadRequestOptions)
                    ?.into(headImage)
//            headImage.setTag(R.id.tag_head,headUrl)
//        }
    }

}